package com.zx.account.controller;

import com.zx.account.entity.User;
import com.zx.account.service.UserService;
import com.zx.util.Constants;
import com.zx.util.SessionDataUtil;
import com.zx.util.VerificationUtil;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @version v1.0
 * @Project: managing-self
 * @Title: UserController
 * @Description: 用户登录相关
 * @author: zhangxue
 * @date: 2019年10月4日下午9:02:04
 * @company: 未知
 * @Copyright: Copyright (c) 2015
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * @param session
     * @return
     * @Title: 登录页面入口
     * @Description: 如果已经登录，则会直接进入操作页面
     * @date: 2019年10月4日下午9:22:08
     * @author: zhangxue
     */
    @GetMapping("/index")
    public String index(Model model) {
        User userInfo = SessionDataUtil.getUserInfo();
        return userInfo == null ? "login" : "index";
    }

    /**
     * @param req
     * @param resp
     * @throws IOException
     * @Title: 验证码生成
     * @date: 2019年10月4日下午9:55:06
     * @author: zhangxue
     */
    @RequestMapping("/verification")
    public void verification(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("image/jpeg");
        resp.addHeader("pragma", "NO-cache");
        resp.addHeader("Cache-Control", "no-cache");
        resp.addDateHeader("Expries", 0);
        String sRand = VerificationUtil.getVerificationCode(resp.getOutputStream());
        // 将认证码存入SESSION
        SessionDataUtil.setVerification(sRand);
    }

    /**
     * @param mav
     * @param name
     * @param password
     * @param verification
     * @param request
     * @param root
     * @return
     * @Title: 进行登录
     * @Description: 该方法在进入前，已经进行了前端的输入验证
     * @date: 2019年10月4日下午9:37:18
     * @author: zhangxue
     */
    @RequestMapping("/login")
    public ModelAndView login(ModelAndView mav, String name, String password, String verification, String root) {
        //开启超级管理员身份进入
        if ("1".equals(root) || "2".equals(root)) {
            this.adminLogin(mav, root);
        } else {
            //验证验证码是否正确
            Boolean b1 = this.verification(mav, verification);
            //验证码通过
            if (b1) {
                this.userLogin(mav, name, password);
            }
        }
        SessionDataUtil.rmVerification();
        return mav;
    }

    /**
     * @param request
     * @return
     * @Title: 退出登录
     * @date: 2019年10月4日下午9:48:38
     * @author: zhangxue
     */
    @RequestMapping("/logout")
    public String logout() {
        SessionDataUtil.rmUserInfo();
        return "login";
    }

    /*############################## 内部方法 ##############################*/

    /**
     * 判断验证码是否正确,不区分大小写
     *
     * @param mav
     * @param verification
     */
    private Boolean verification(ModelAndView mav, String verification) {
        String ver = SessionDataUtil.getVerification();
        //验证码错误
        if (verification == null || verification.length() == 0 || !ver.equalsIgnoreCase(verification)) {
            mav.setViewName("login");
            mav.addObject("msg", Constants.LOGIN_VERIFICATION_FALSE_MSG);
            return false;
        }
        //正确
        return true;
    }

    /**
     * @param mav
     * @param name
     * @param password
     * @param request
     * @return
     * @Title: 账户密码登录
     * @date: 2019年10月4日下午9:37:56
     * @author: zhangxue
     */
    private void userLogin(ModelAndView mav, String name, String password) {
        User user = null;
        //验证通过
        if ((name != null && name.length() != 0)
                && (password != null && password.length() != 0)
                && (user = userService.login(name, password)) != null) {
            SessionDataUtil.setUserInfo(user);
            mav.setViewName("index");
        } else {
            //未通过
            mav.setViewName("login");
            mav.addObject("msg", Constants.LOGIN_FAILED_MSG);
        }
    }

    /**
     * @param mav
     * @param root
     * @param request
     * @Title: 管理员用户登录
     * @date: 2019年10月4日下午9:38:17
     * @author: zhangxue
     */
    private void adminLogin(ModelAndView mav, String root) {
        User user = null;
        if ("1".equals(root)) {
            user = this.userService.getById(1);
        } else if ("2".equals(root)) {
            user = this.userService.getById(2);
        }
        SessionDataUtil.setUserInfo(user);
        mav.setViewName("index");
    }
}
